Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
¿Por qué obtengo la edad [i] como indefinida?

Aquí voy a describir mi problema de por qué obtengo age[i]=a como indefinido cuando voy a consolarlo

ingrese la descripción de la imagen aquí

 const birthYear = [1996, 1997, 1998, 1999] function calc(Year) { age = 2022 - Year; return age } var age = [] age[0] = 123; console.log(age[0]) for (i = 0; i <= birthYear.length - 1; i++) { var a = calc(birthYear[i]) /* if you want to return the value of any function you have to address it the help of any agruement otherwise it will show you a function body */ age[i] = a // console.log console.log(age[i]) } // console.log(age[])

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

El problema es que devuelve "edad" con calc y también llama "edad" a su matriz. Por lo tanto, la matriz se reemplaza cada vez y obtienes "indefinido".

Prueba esto :

 const birthYear=[1996,1997,1998,1999] function calc(Year) { age=2022-Year; return age } var ageArr = []; for(var i = 0 ; i < birthYear.length ; i++){ ageArr[i] = calc(birthYear[i]) console.log(ageArr[i]); }
about 4 years ago · Juan Pablo Isaza Report

0

¿Por qué obtengo la edad [i] como indefinida?

ha utilizado la palabra clave var para declarar la matriz de edad, ahora la edad se convierte en una variable global, por lo que cuando realiza una llamada a la función calC, convierte la matriz en un número de age=2022-Year; después de ejecutar esta sentencia. esa es la razón por la que age[i] no está definida. ahora cuál es la forma correcta de hacer esto, simplemente declare ageCount dentro de la función calc y devuélvalo, así

 const birthYear=[1996,1997,1998,1999] function calc(Year) { //instead of using 2022, you fetch current year from date object let ageCount=2022-Year; return ageCateCount } var age=[] age[0]=123; console.log(age[0]) for(i=0;i<=birthYear.length-1;i++){ age[i]=calc(birthYear[i]); // console.log console.log(age[i]) } 

 const birthYear = [1996, 1997, 1998, 1999] function calc(Year) { let ageCount = new Date().getFullYear() - Year; return ageCount } var age = [] age[0] = 123; console.log(age[0]) for (i = 0; i <= birthYear.length - 1; i++) { var a = calc(birthYear[i]) /* if you want to return the value of any function you have to address it the help of any agruement otherwise it will show you a function body */ age[i] = a // console.log console.log(age[i]) }

espero que esto ayude!

about 4 years ago · Juan Pablo Isaza Report

0

Si entiendo el requisito correctamente, desea encontrar la diferencia entre el año 2022 y los años en la matriz birthYear . En caso afirmativo, simplemente puede lograr esto con una sola línea de código en lugar de hacerlo complejo.

demostración :

 const birthYear = [1996, 1997, 1998, 1999]; const age = birthYear.map(year => 2022 - year); console.log(age);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!